// pages/transportInformation/change/change.js
const http = require("../../../utils/request");
let app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datas:{},
    value: "",
    breed:"",
    amount:0,
    type: {
      cultivationCode: "养殖批次",
      count: "分拨数量（只）",
      destination: "货品去向",
      licenseNumber: "车牌号码",
      driver: "司机",
      remark: "备注",
    }
  },
  change(e){
    console.log(e);
    let current = e.currentTarget.dataset.data;
    let datas = this.data.datas;
    datas[current] = e.detail.value;
    this.setData({
      datas,
    })
    if(current==='cultivationCode'&&app.operation===1){
      //TODO:先用put才能获取分拨数量
      http.getRequest('/admin/cultivation/'+e.detail.value+'/residue',{},res=>{
        console.log(res)
        console.log(1,res.data.data);
        if(!res.data.success){
          this.setData({
            amount:0
          })
        }else{
          this.setData({
            amount: res.data.data
          })
        }
      },err=>{});
    }
  },
  confirm(){
    console.log(JSON.stringify(this.data.datas));
    if(app.operation===1){
      http.postRequestBody('/admin/dispatch',JSON.stringify(this.data.datas),res=>{
        wx.navigateBack({
          delta: 1,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      },err=>{});
    }else{
      http.putRequest('/admin/dispatch',JSON.stringify(this.data.datas),res=>{
        wx.navigateBack({
          delta: 1,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      },err=>{});
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(app.operation===0){
      this.setData({
        datas: app.trans_data,
        amount: app.trans_data['count']
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})